package com.itown.expo.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.dao.DataAccessException;
import org.springframework.security.GrantedAuthority;
import org.springframework.security.GrantedAuthorityImpl;
import org.springframework.security.userdetails.UserDetails;
import org.springframework.security.userdetails.UsernameNotFoundException;

import webframe.common.helper.FormatTransformHelper;

import com.itown.expo.model.EntUserInfo;

public class EntUserService extends UserService {
	@Override
	public UserDetails loadUserByUsername(String userName)
			throws UsernameNotFoundException, DataAccessException {
		String username = FormatTransformHelper.transEncodingFormBrowser(
				userName, "utf-8");
		EntUserInfo user = findEntUserByUsername(username);
		if (user == null) {
			throw new UsernameNotFoundException("用户名或密码错误");
		}
		getEntUserInfoDao().getHibernateTemplate().getSessionFactory()
				.getCurrentSession().evict(user);
		List<GrantedAuthority> gas = new ArrayList<GrantedAuthority>();
		gas.add(new GrantedAuthorityImpl("ROLE_ENTUSER"));
		user.setAuthorities(gas.toArray(new GrantedAuthority[] {}));
		return user;
	}
}
